package com.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.config.AlipayConfig;
import com.entry.AliPay;
import com.entry.OrderForm;
import com.entry.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.server.OrderFormService;
import com.server.ShoppingService;
import com.util.RedisUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AlipayController {
    @Resource
    RedisUtil redisUtil;
    @Resource
    OrderFormService orderFormService;
    @Resource
    ShoppingService shoppingService;

    @RequestMapping("/homepay")
    public String index(HttpSession session, Model model) {
        String order_code=(String) redisUtil.get("ordr_code");
        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        //redisUtil.set("shopping_checkedCar","1");
        //redisUtil.set("shopping_checkedCarS","3");
        if (redisUtil.get("shopping_checkedCar")!=null){
            String shopping_checkedCar1=(String) redisUtil.get("shopping_checkedCar");
            int shopping_checkedCar= Integer.parseInt(shopping_checkedCar1);
            if (shopping_checkedCar==1){
                System.out.println("1");
            }
            //int a=shoppingService.delCartProductByChecked(shoppingListPay.get(i).getMember_id(), shoppingListPay.get(i).getProduct_id());
            int a=shoppingService.delCartProductByCheckeds(member_id, shopping_checkedCar);

        }
        if (redisUtil.get("shopping_checkedCarS")!=null){
            String shopping_checkedCarS1=(String) redisUtil.get("shopping_checkedCarS");
            int shopping_checkedCarS= Integer.parseInt(shopping_checkedCarS1);
            if (shopping_checkedCarS==3){
                System.out.println("3");
            }
            int a=shoppingService.delCartProductByCheckeds(member_id, shopping_checkedCarS);
        }
        /*int order_code= Integer.parseInt(order_code1);
        System.out.println(order_code);*/
        OrderForm orderForm=orderFormService.getOrderFormByOrderCode(order_code);
        session.setAttribute("orderForm",orderForm);
        model.addAttribute("orderForm",orderForm);
        return "pay/homepay";
    }


    @RequestMapping("/return_url")
    public String return_url(HttpSession session){
        /*int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        //当前时间日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date d = new Date(System.currentTimeMillis());
        //redies缓存购物车支付ordr_code
        //redisUtil.set("ordr_code_shop",ordr_code);
        if (redisUtil.get("ordr_code_shop")!=null){
            String  ordr_code_shop= String.valueOf(redisUtil.get("ordr_code_shop"));
            orderFormService.modifyOrderStatus_pay_date(0, d,ordr_code_shop);
            System.out.println("已支付状态:订单号="+ordr_code_shop);
            redisUtil.delete("ordr_code_shop");
        }

        //redies缓存立即购买支付ordr_code
        //redisUtil.set("ordr_code_liji",ordr_code);
        if (redisUtil.get("ordr_code_liji")!=null){
            String  ordr_code_liji= String.valueOf(redisUtil.get("ordr_code_liji"));
            orderFormService.modifyOrderStatus_pay_date(0, d,ordr_code_liji);
            System.out.println("已支付状态:订单号="+ordr_code_liji);
            redisUtil.delete("ordr_code_liji");

        }

        if (redisUtil.get("member_id")!=null){
            int user_id=member_id;
            List<OrderForm> orderFormList=orderFormService.getOrderSellandStatus(user_id, 0);
            orderFormList.forEach(System.out::println);
            session.setAttribute("orderFormList",orderFormList);
        }*/
        return "order";
    }
    @RequestMapping("/notify_url")
    @ResponseBody
    public Result notify_url(HttpSession session){
        Result result=new Result();
        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        //当前时间日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date d = new Date(System.currentTimeMillis());
        //redies缓存购物车支付ordr_code
        //redisUtil.set("ordr_code_shop",ordr_code);
        if (redisUtil.get("ordr_code_shop")!=null){
            String  ordr_code_shop= String.valueOf(redisUtil.get("ordr_code_shop"));
            orderFormService.modifyOrderStatus_pay_date(0, d,ordr_code_shop);
            System.out.println("已支付状态:订单号="+ordr_code_shop);
            redisUtil.delete("ordr_code_shop");
        }

        //redies缓存立即购买支付ordr_code
        //redisUtil.set("ordr_code_liji",ordr_code);
        if (redisUtil.get("ordr_code_liji")!=null){
            String  ordr_code_liji= String.valueOf(redisUtil.get("ordr_code_liji"));
            orderFormService.modifyOrderStatus_pay_date(0, d,ordr_code_liji);
            System.out.println("已支付状态:订单号="+ordr_code_liji);
            redisUtil.delete("ordr_code_liji");

        }

        if (redisUtil.get("member_id")!=null){
            int user_id=member_id;
            List<OrderForm> orderFormList=orderFormService.getOrderSellandStatus(user_id, 0);
            orderFormList.forEach(System.out::println);
            session.setAttribute("orderFormList",orderFormList);
        }
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "/payinfo",method = RequestMethod.POST)
    public void aliPay(AliPay aliPay, HttpServletRequest request, HttpServletResponse response) throws IOException {

        //初始化参数对应配置类AlipayConfig属性
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //创建API对应的request
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        //在公共参数中设置回跳和通知地址
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //将对象转化为json格式
        ObjectMapper objectMapper = new ObjectMapper();
        String content = objectMapper.writeValueAsString(aliPay);
        //请求参数集合
        alipayRequest.setBizContent(content);


        //填充业务参数
        String form = "";
        try {
            //调用SDK生成表单
            form = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + AlipayConfig.charset);

        //直接将完整的表单html输出到页面
        response.getWriter().write(form);
        response.getWriter().flush();
        response.getWriter().close();


    }
}
